	/**
	 * 为各个按钮添加click功能 
	 */
	  window.onload = function(){
	    document.getElementById( "add-left" ).onclick = addLeft;
		document.getElementById( "add-right" ).onclick = addRight;
		document.getElementById( "del-left" ).onclick = delLeft;
		document.getElementById( "del-right" ).onclick = delRight;
		document.getElementById( "bubble-sort" ).onclick = bubbleSort;
		document.getElementById( "sel-sort" ).onclick = selectionSort;
	  }	  

	var counter = 0;  //记录输入数字的个数
	/**
	 * 左侧添加数字
	 */	
	function addLeft(){
	  var num = document.getElementById( "in-num" ).value;
	  if( num ){
	    if( counter >= 60){
		  alert( "最多只能输入60个数字" );
		}
	    else{ 
		  if( num>100 || num<10){
		    alert("输入的值的必须在10和100之间");
		  }else{
		    counter++;
	        var node_div = document.createElement( "div" );
	        var node_parent = document.getElementById( "parent" );
	        node_div.className = "number";
	        node_div.style.left = "10px";
	        node_div.style.height = num*2 +"px";
	        if ( !node_parent.children ){
	          node_parent.appendChild( node_div );
	        }else{
		      var nodelist = document.getElementById( "parent" ).children;
		      for( var i=0 ; i<nodelist.length ;i++){
		        var aa = nodelist[i].style.left;
			    nodelist[i].style.left = ( parseInt( aa,10 )+10 )+"px"; 
		      }
	          var fir_node = node_parent.firstChild;
		      node_parent.insertBefore( node_div,fir_node );
			  document.getElementById( "in-num" ).value = Math.floor(Math.random()*90+10);
		    }
		  }
	    }
	  }
	}
	
	/**
	 * 右侧添加数字
	 */
	function addRight(){
	  var num = document.getElementById( "in-num" ).value;
	  if( num ){
	    if( counter>=60){
	      alert( "最多只能输入60个数字" );
        }else{
		  if( num>100 || num<10){
		    alert("输入的值的必须在10和100之间");
		  }else{
		    counter++;
			var node_div = document.createElement( "div" );
	        var node_parent = document.getElementById( "parent" );
			var node_len = node_parent.children.length;
	        node_div.className = "number";
	        node_div.style.left = (node_len+1)*10 +"px";
	        node_div.style.height = num*2 +"px";
			node_parent.appendChild( node_div );
		  }
		}
	  }
	}

	/**
	 * 删除左侧数字
	 */
	function delLeft(){
	  var node_parent = document.getElementById( "parent" );
	  if( node_parent.firstChild ){
	    var nodelist = node_parent.children;
		for( var i=0 ; i<nodelist.length ;i++){
		  var aa = nodelist[i].style.left;
		  nodelist[i].style.left = ( parseInt( aa,10 )-10 )+"px"; 
		}
	    node_parent.removeChild( node_parent.firstChild );
		counter--;
	  }
	}
	
	/**
	 * 删除右侧数字
	 */
	function delRight(){
	  var node_parent = document.getElementById( "parent" );
	  if( node_parent.lastChild ){
	    node_parent.removeChild( node_parent.lastChild );
		counter--;
	  }
	}
			document.getElementById( "bubble-sort" ).onclick = function(){};
	/**
	 * 冒泡排序
	 */
	function bubbleSort(){
	  var i = 0;
	  var j = 0;
	  var time1 = setInterval( function(){
	  	var nodelist = document.getElementById( "parent" ).children;
	    var len = nodelist.length;
	    var num1 = parseInt( nodelist[j].style.height,10 );
        var num2 = parseInt( nodelist[j+1].style.height,10 );
		var m = j+1;
		    n = j+2;
		var str = "第"+m+"个数与第"+n+"个数作比较";
		document.getElementById( "ifo" ).innerHTML = str;
	    if( num1 > num2){
		  nodelist[j].style.height = num2 +"px";
		  nodelist[j+1].style.height = num1 +"px";
	    }
		j++;
	    if( j==(len-i-1) ){
	      i++;
		  j=0;
	    }
	    if( i==(len-1) ){
	      clearInterval( time1 );
		  document.getElementById( "ifo" ).innerHTML = "冒泡排序完成";
	    }
	  },500);
	}

	/**
	 * 选择排序
	 */
	function selectionSort(){
	  var i = 0;
	  var j = 0;
	  var time2 = setInterval( function(){
	    var nodelist = document.getElementById( "parent" ).children;
		var len = nodelist.length;
		var num1 = parseInt( nodelist[j].style.height,10 );
		for ( var m=j+1;m<len;m++){
		  var num2 = parseInt( nodelist[m].style.height,10 );
		  if(num2<num1){
		    num1 = num2;
		    j = m;
		  }
		}
		nodelist[j].style.height = parseInt( nodelist[i].style.height,10 ) +"px";
		nodelist[i].style.height = num1 + "px";
		i++;
		var str = "第"+i+"个数与第"+j+"个数作交换";
		document.getElementById( "ifo" ).innerHTML = str;
		j=i;
		if( i==len-1){
		  document.getElementById( "ifo" ).innerHTML = "选择排序完成";
		  clearInterval( time2 );
		}  
	  },500);
	}